package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: liangyuanzhang
 * Date: 2025-3-25
 * Time: 22:00
 */
public class MySingleList implements IList{
    static class ListNode{ // 创建一个内部类 ==> 链表(单)
        public int val; // 节点值域
        public ListNode next; // 节点指针域

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head; // 默认为null

    @Override
    // 头插
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head; // 让后面节点和头节点进行链接
        head = node;
    }

    @Override
    // 尾插
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null){ // while 循环正是为了找到链表的最后一个节点，然后将新节点插入到尾部。
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if (index < 0 || index > len){
            System.out.println("index位置不合法!");
            return;
        }
        if (index == 0){ // 头插
            addFirst(data);
            return;
        }
        if (index == len){ // 尾插
            addLast(data);
            return;
        }
        // 中间插入
        ListNode cur = head;
        while (index -1 != 0){  //这个循环在单链表中找到要插入位置的前驱节点
            cur = cur.next;  // 让 cur 成为被插入节点的前驱节点
            index--;
        }
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head == null){
            return;
        }
        if (head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = findNodeOfKey(key); // 找到值为key的节点的前驱节点
        if (cur == null){
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findNodeOfKey(int key){
        ListNode cur = head;
        while (cur.next != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if (head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        /*
        运用两个指针prev和cur分别指向head和head.next向后遍历删除key
         */
        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key){
            head = head.next;
        }
    }

    @Override
    public int size() {
        int len = 0;
        ListNode cur = head;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }
}
